//
//  HomeAdView.m
//  51易借
//
//  Created by apple on 2017/9/15.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "HomeAdView.h"
#import "HomeAdCollectionViewCell.h"
@implementation HomeAdView
{
    NSInteger index;
}

-(id)init
{
    self = [super initWithFrame:CGRectZero collectionViewLayout:[UICollectionViewFlowLayout new]];
    if(self)
    {
        [self setUp];
    }
    return self;
}

-(void)setUp
{
    self.delegate = self;
    self.dataSource = self;
    self.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeIndex) name:systemTimer object:nil];

}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)changeIndex
{
    index++;
    if(index%3==0){
        [self setContentOffset:CGPointMake(0, index/3 * self.frame.size.height) animated:YES];
        if(index/3 == self.models.count)
        {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if(index/3 == self.models.count)
                {
                    index = 0;
                    [self setContentOffset:CGPointZero animated:NO];
                    
                };
            });
        }
    }
    
}

-(void)setModels:(NSArray *)models
{
    _models = models;
    index = 0;
    [self setContentOffset:CGPointZero animated:NO];
    
    [self reloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _models.count+1;
}
-(__kindof UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeAdCollectionViewCell * cell = [HomeAdCollectionViewCell cellForCollectionView:collectionView andIndexPath:indexPath andIdentifier:@"cell"];
    if(indexPath.row+1<= _models.count)
        cell.model = self.models[indexPath.row];
    else
        cell.model = self.models[0];
    
    return cell;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.frame.size.width, self.frame.size.height);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.clickBlock)
    {
        HomeAdModel * model;
        if(indexPath.row+1<= _models.count)
            model = self.models[indexPath.row];
        else
            model = self.models[0];
        self.clickBlock(model);
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
